﻿/// <reference path="../jquery-1.6.2-vsdoc.js" />

/// <reference path="core.js" />

(function ($) {

    $.extend({
        Kaio: {

            msg: {},

            Fx: {

                baseUrl: '/flexexplorer/',

                defaults: {
                    callback: null
                },


                render: function (opts) {

                    var _imgs = opts.files.split(';');

                    var _imgPanel = $(opts.targetId);

                    $.each(_imgs, function (index, value) {

                        var _id = value.replace(".", "_").replace("/", "_");

                        if ($('#_img_' + _id).length == 0) {
                            var _html = '<li onmouseover="$(\'#_action_img_' + _id + '\').show();" onmouseout="$(\'#_action_img_' + _id + '\').hide();" class="rc img-thumb" id="_img_' + _id + '"><div>';
                            _html += '<input type="hidden" name="img" value="' + value + '" />';
                            _html += '<img class="thumb-50" src="' + $.Nasa8x.imagePrefix + value + '&size=50" />';
                            _html += '<div id="_action_img_' + _id + '" class="action"><a href="javascript:void(0)" class="icon-16 ok" title="Chọn là ảnh chính" onclick="$.Nasa8x.moveFirst(\'#_img_' + _id + '\',\'#_imagePreview\');" >&nbsp;</a><a href="javascript:void(0)" title="Xóa ảnh này" class="icon-16 del" onclick="$.Nasa8x.remove(\'#_img_' + _id + '\');" >&nbsp;</a></div>';
                            _html += '</div></li>';

                            _imgPanel.append(_html);
                        }

                    });

                    _imgPanel.show();

                },
                open: function (opts) {

                    var url = this.baseUrl + '?fx=1';

                    var w = opts.width > 0 ? opts.width : screen.width * 0.8;
                    var h = opts.height > 0 ? opts.height : screen.height * 0.8;

                    var winX = ($(window).width() - w) * .5;
                    var winY = ($(window).height() - h) * .5;

                    //

                    window.showModalDialog(url, opts, 'dialogWidth=' + w + ',dialogHeight=' + h + ',center=yes,left=' + winX + ',top=' + winY);

                }


            },


            toggleCheckbox: function (_state, _forClass) {

                // default
                var _class = 'input.selected';

                if (_forClass) _class = _forClass;

                $(_class).each(function () {
                    $(this).attr("checked", _state);
                });

            },
            getSelectedItems: function (forClass) {
                var items = new Array();

                var _class = 'input.selected';

                if (forClass) _class = forClass;
                // $("input[@class='selected']:checked").each(function() {
                $(_class).each(function () {
                    //if ($(this).attr("checked") == true) {
                    if ($(this).is(":checked")) {
                        items.push($(this).val());
                    }
                });
                return items;

            },

            reset: function () {

                // Set class for button

                /*  $(':button').hover(
                function () {
                if (!$(this).hasClass('no-addclass'))
                $(this).addClass('button-over');
                },
                function () {
                $(this).removeClass('button-over');
                });

                $(':input')
                .addClass('input').focus(function () {
                if (!$(this).hasClass('no-addclass')) {
                $(this).addClass('input-focus');
                }
                }).blur(function () {
                $(this).removeClass('input-focus');
                });*/

                $('input:button,input:submit').hover(
                    function () {
                        if (!$(this).hasClass('no-addclass'))
                            $(this).addClass('button-over');
                    },
                    function () {
                        $(this).removeClass('button-over');
                    });


                //Set class for textbox input:file,
                $('input:text,input:password,textarea')
                        .focus(function () {
                            if (!$(this).hasClass('no-addclass')) {
                                $(this).addClass('input-focus');
                            }
                        }).blur(function () {
                            $(this).removeClass('input-focus');
                        });
            },


            autoReload: function () {
                if (this.minutePageRefresh > 1) {
                    window.setTimeout("window.location.reload()", this.minutePageRefresh * 60 * 1000);
                }

            },




            logout: function () {

                $.Overlay.show();
                var ht = document.getElementsByTagName("html");
                ht[0].style.filter = "progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)";
                if (confirm($.Nasa8x.messages.logoutConfirm)) {
                    return true;
                }
                else {
                    ht[0].style.filter = "";
                    $.Overlay.hide();
                    return false;

                }
            },
            addJs: function (_url) {

                var headID = document.getElementsByTagName("head")[0];
                var newScript = document.createElement("script");
                newScript.type = "text/javascript";
                newScript.src = _url;
                headID.appendChild(newScript);
            },

            addCss: function (_url) {

                var fileref = document.createElement("link");
                fileref.setAttribute("rel", "stylesheet");
                fileref.setAttribute("type", "text/css");
                fileref.setAttribute("href", _url);

                if (typeof fileref != "undefined")
                    document.getElementsByTagName("head")[0].appendChild(fileref);

            },

            newWindow: function (url, name, width, height) {

                var winW = $(window).width();
                var winH = $(window).height();
                var winX = 0;
                var winY = 0;
                var w = width > 0 ? width : winW * 0.8;
                var h = height > 0 ? height : winH * 0.8;

                winX = (winW - w) * .5;
                winY = (winH - h) * .5;
                window.open(url, name, 'scrollbars,resizable=no, width=' + w + ',height=' + h + ',left=' + winX + ',top=' + winY);
            },


            showActionArea: function () {
                $('#detail-actions-area-container').slideDown();
            },

            closeActionArea: function () {

                $('#detail-actions-area-container .child').attr('rel', 0);
                $('#detail-actions-area-container').slideUp();
            },

            share: function (title, url) {

                var _el = $('#shareIt');
                if (_el.attr('rel') != 1) {
                    _el.attr('rel', 1).show();
                    $.shareIt.show({ title: title, url: url });
                    this.showActionArea();
                }
                else {
                    _el.hide();
                    this.closeActionArea();
                }

            },

            report: function () {

                var _el = $('#reportIt');
                if (_el.attr('rel') != 1) {
                    _el.attr('rel', 1).show();
                    this.showActionArea();
                }
                else {
                    _el.hide(); this.closeActionArea();
                }


            },


            switchView: function (_v) {

                if (_v) {
                    $.Cookie.add('__view', _v);
                }
                else {
                    _v = $.Cookie.get('__view');
                };

                if (!$.String.isNullOrEmpty(_v)) {
                    $('ul.action li').removeClass("actived");
                    $('#view-' + _v).addClass('actived');
                    $('#_listPanel').removeClass().addClass(_v + '-view');
                }


            },

            /* cmd: function (c, callback) {

            $("#cmd").val(c);
            this.ajaxSubmit('_fcmd', callback);
            },*/

            //{ name: "John", time: "2pm" }
            cmd: function (params, callback) {

                $.Loading.show();

                $.post("/Cpanel/Cmd", params, function (response) {
                    var d = (typeof response) == 'string' ? eval('(' + response + ')') : response;

                    if (callback) {
                        if ($.isFunction(callback))
                            callback.call(this, d);
                        else
                            eval(callback);
                    }
                    else {
                        $.Ajax.success(d);
                    }

                    $.Loading.hide();

                }, 'json');

            },


            ajaxSubmit: function (f) {

                if (f)
                    $.Ajax.submit(f);
                else {

                    $('form.ajax').unbind('.kaio').bind("submit.kaio", function () {
                        $.Ajax.submit($(this));
                        return false;
                    });
                }

            },


            ajaxLoad: function (url) {

                if (url)
                    $.Ajax.load(url);
                else {
                    $('a.ajax').unbind('.kaio').bind("click.kaio", function () {
                        var a = $(this);
                        var o = $.extend({}, $.metadata ? a.metadata() : $.meta ? a.data() : {});
                        $.Ajax.load(a.attr('href'), o);
                        return false;
                    });
                }

            },


            onLoad: function () {

                this.reset();
                this.ajaxSubmit();
                this.ajaxLoad();
                this.switchView();

            }
            // add more function

        }// end Nasa8x
    });   // end extend

})(jQuery);

